package com.yqb.study.common.mq;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.yqb.study.util.FastJsonUtil;

@Component
public class MqSender {

	private static Logger log = LoggerFactory.getLogger(MqSender.class);

	@Autowired
	AmqpTemplate amqpTemplate;

	public void sendFanout(MqMessage message) {
		String msg = FastJsonUtil.toJson(message);
		log.info("send fanout message:" + msg);
		amqpTemplate.convertAndSend(message.getExchange(), "", msg);
	}
	
	public void sendDirect(MqMessage message) {
		String msg = FastJsonUtil.toJson(message);
		log.info("send direct message:" + msg);
		amqpTemplate.convertAndSend(message.getExchange(), message.getRouteKey(), msg);
	}	
	
	public void sendTopic(MqMessage message) {
		String msg = FastJsonUtil.toJson(message);
		log.info("send topic message:" + msg);
		amqpTemplate.convertAndSend(message.getExchange(), message.getRouteKey(), msg);
	}	

	//
	// public void sendHeader(Object message) {
	// String msg = RedisService.beanToString(message);
	// log.info("send fanout message:"+msg);
	// MessageProperties properties = new MessageProperties();
	// properties.setHeader("header1", "value1");
	// properties.setHeader("header2", "value2");
	// Message obj = new Message(msg.getBytes(), properties);
	// amqpTemplate.convertAndSend(MQConfig.HEADERS_EXCHANGE, "", obj);
	// }

}
